// 显示下载队列状态
function updateQueueStatus() {
    chrome.runtime.sendMessage({ action: 'getQueueStatus' }, function(response) {
        if (response) {
            document.getElementById('queueStatus').textContent = `等待下载: ${response.queueLength}`;
        }
    });
}

// 初始更新队列状态
updateQueueStatus();
// 每秒更新一次队列状态
setInterval(updateQueueStatus, 1000);

// 监听清除按钮点击事件
document.getElementById('clearBtn').addEventListener('click', function() {
    const statusElement = document.getElementById('status');
    statusElement.textContent = '正在清除...';

    // 向background发送清除记录请求
    chrome.runtime.sendMessage(
        { action: 'clearDownloadHistory' },
        function(response) {
            if (response && response.status === 'success') {
                statusElement.textContent = '下载记录已清除';
                // 2秒后自动关闭弹窗
                setTimeout(() => window.close(), 2000);
            } else {
                statusElement.textContent = '清除失败，请重试';
            }
        }
    );
});